package com.zzld.modules.autoinn.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Area;
import com.zzld.modules.autoinn.service.IAreaService;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.TreeGrid;
import com.zzld.modules.sys.security.ShiroUser;

@Controller
@RequestMapping("/autoinn/area")
public class AreaController {

	@Autowired
	private IAreaService areaService;

	private String areaurl = "/modules/autoinn/area/areaList";
	private String areaCruurl = "modules/autoinn/area/areaCru";

	@RequestMapping(method = RequestMethod.GET)
	public String area() {
		return areaurl;
	}

	// 进入修改页面
	@RequestMapping(value = "cru")
	public String update(Model model, HttpServletRequest request) {
		return areaCruurl;
	}

	/*
	 * 修改 添加操作
	 */

	@RequestMapping(value = "upte/pre", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON upordate(HttpServletRequest request, Area a, Model model) {
		CommonJSON json = new CommonJSON();

		ShiroUser userq = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(a.getId())) {// 新增

			a.setLevel(levl(a.getPid()));
			a.setCreate_by(userq.getName());
			a.setCreate_date(new Date());
			a.setId(UuidUtil.uuid().toString());
			if (areaService.uporadd(a, 0) > 0) {
				json.setMessage(MessageUtils.ADD_SUCCESS);
				json.setSuccess(true);
			} else {
				json.setMessage(MessageUtils.ADD_FAIL);
				json.setSuccess(false);
			}
		} else {
			a.setLevel(levl(a.getPid()));
			if (areaService.uporadd(a, 1) > 0) {
				json.setMessage(MessageUtils.EDIT_SUCCESS);
				json.setSuccess(true);
			} else {
				json.setMessage(MessageUtils.EDIT_WARING);
				json.setSuccess(false);
			}
		}

		return json;
	}

	// 删除操作
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON delete(Model model, HttpServletRequest request,
			@PathVariable("id") String id) {
		CommonJSON json = new CommonJSON();
		if (areaService.sercount(id) > 0) {
			json.setMessage("请删除子节点!");
			json.setSuccess(false);
		} else {
			if (areaService.deletearea(id) > 0) {
				json.setMessage(MessageUtils.DELETE_ALL_SUCCESS);
				json.setSuccess(true);
			} else {
				json.setMessage(MessageUtils.DELETE_ALL_FAIL);
				json.setSuccess(false);
			}
		}
		return json;
	}

	@RequestMapping(value = "treegrid")
	@ResponseBody
	public List<TreeGrid> treegrid(HttpServletRequest request, Model model) {
		return areaService.treeGrid();
	}

	@RequestMapping(value = "areaser", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject sebyid(HttpServletRequest request, Model model) {
		JSONObject json = new JSONObject();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Area a = areaService.selectbyid(request.getParameter("id"));
			map.put("id", a.getId());
			map.put("areaname", a.getAreaname());
			map.put("pid", a.getPid());
			map.put("islast", a.getIslast());
			map.put("sort", a.getSort());
			map.put("state", a.getState());
			json.putAll(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	public String levl(String pid) {
		String levl = "";
		if (!pid.equals("") && pid != null) {
			levl = areaService.selectbyid(pid).getLevel();
			Integer q = Integer.parseInt(levl);
			q++;
			levl = q.toString();
		} else {
			levl = "1";
		}
		return levl;
	}

}
